package com.example.giftrecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.giftrecord.entity.GiftRecord;
import com.example.giftrecord.mapper.GiftRecordMapper;
import com.example.giftrecord.service.IGiftRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 礼金记录表 服务实现类
 * </p>
 *
 * @author example
 * @since 2023-07-15
 */
@Service
public class GiftRecordServiceImpl extends ServiceImpl<GiftRecordMapper, GiftRecord> implements IGiftRecordService {

    @Override
    public List<GiftRecord> getAllRecords() {
        // 添加查询条件，按创建时间倒序排列
        QueryWrapper<GiftRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("created_at"); // 按创建时间降序
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public GiftRecord getRecordById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean addRecord(GiftRecord giftRecord) {
        // 设置创建时间
        giftRecord.setCreatedAt(LocalDateTime.now());
        return baseMapper.insert(giftRecord) > 0;
    }

    @Override
    public boolean updateRecord(GiftRecord giftRecord) {
        return baseMapper.updateById(giftRecord) > 0;
    }

    @Override
    public boolean deleteRecord(Long id) {
        return baseMapper.deleteById(id) > 0;
    }
}
    